<?php
//网络教研平台数据抓取
class yx_data extends control
{
	public function __construct()
	{
		header("Content-Type: text/html; charset=utf-8");
		set_time_limit(0);//设置不超时.		
		parent::__construct();
		ini_set('memory_limit',-1);

		$this->xcycwl = 'http://xuchangyancao.7.xcycwl.com/';
		$this->p =  $this->app->loadClass('proving');//加载相关类
		
		$this->ip = 'http://10.64.1.88';
		$this->v6 = 'http://10.64.1.88/v6/';
		$this->custclassification = 'http://10.64.1.88/crm/custclassification/';
		$this->custmanagement = 'http://10.64.1.88/crm/custmanagement/';
		$this->greport = 'http://10.64.1.88/rs/greport/';
	}
	
	//获取样本规格订单数		localhost/get_data/pds/yx_data-get_customer_info
	public function get_customer_info(){
		$sale = 'http://10.64.1.88/co/sale/';
		set_time_limit(0);
		header("Content-Type: text/html; charset=utf-8");
		
		$j_username = 'pdsxyg';
		$j_password = 'baa98442f86f17c171d71884f845a288';
		$this->index($j_username,$j_password);
		
		//滚动条设置始终在最低端
		echo '<script src="../theme/js/jquery-1.11.3.min.js"></script>';
		echo '<div class="scorpall" style="margin-bottom:60px;">';
		echo '</div>';
		
		//$dos = $this->getyborders();		//客户信息
	
		$this->get_order1();		//获取订单
		
		//$this->getcustomerinfo();		//获取诚信等级
		
	}
	
		
	//获取样本规格订单数		localhost/get_data/pds/yx_data-get_cs_info
	public function get_cs_info(){
		$sale = 'http://10.64.1.88/co/sale/';
		set_time_limit(0);
		header("Content-Type: text/html; charset=utf-8");
		
		$j_username = 'pdsxyg';
		$j_password = 'baa98442f86f17c171d71884f845a288';
		$this->index($j_username,$j_password);
		
		//滚动条设置始终在最低端
		echo '<script src="../theme/js/jquery-1.11.3.min.js"></script>';
		echo '<div class="scorpall" style="margin-bottom:60px;">';
		echo '</div>';
		
		$dos = $this->getyborders();		//客户信息
		
	}
	
	//获取样本客户订单
	public function getyborders(){
		$cust = $this->yx_data->get_cs_cus();
		//a($cust);exit;
		foreach($cust as $v){
			$gurl = $this->greport.'greport.cmd?method=getReportData';
			$postdata = array();
			$postdata['reportCode'] = '8d03675e-b8da-45f5-9ae8-58b5cbe47c18';  
			$postdata['searcherDescription'] = '';
			$postdata['COM_ID'] = '';
			$postdata['COM_ID_BACKUPS'] = '';
			$postdata['SALE_CENTER_ORG_ID'] = '';
			$postdata['SALE_DPT_ORG_ID'] = '';
			$postdata['commonCustNameSearch'] = '';
			$postdata['commonCustIdSearch'] = $v->license;
			$postdata['custFilterSearch'] = '';
			$postdata['custTagComIdSearch'] = '';
			$postdata['organType'] = '';
			$postdata['custTagWhereSqlSearch'] = '';
			$postdata['custFilterSqlSearch'] = 'U0VMRUNUJTIwQ1VTVF9JRCUyMEZST00lMjBDT19DVVNUJTIwV0hFUkUlMjAxJTIwJTNEJTIwMSUyMCUyMEFORCUyMENPTV9JRCUyMElOJTIwKCcxMTQxMDQwMScpJTIw';
			$postdata['isSelectCustSearch'] = 'Y';
			$postdata['hiddenInputForTitle'] = '报表查询';
			//commonCustIdSearch: 410423107766

			$customer = array();
			$res = $this->data_page1($gurl,$postdata);
			//a($res);exit;
			if($res){
				$customer = json_decode($res);
			}
			
			if(isset($customer->tableBodyData)){
				$tableBodyData = $customer->tableBodyData;
				
				foreach($tableBodyData as $v){
					
					$content = date('H:i:s').'----------------1商户----'.$v[3]->value.'-------'.$v[1]->value.'<br />';
					$this->yx_data->push_div_content('.scorpall',$content); 
					
					$this->yx_data->getyborders($v);
				}
				
			}
		
		}
		
		echo '<span style="color:red;font-size:26px;"> 同步完成</span>';
		//a($customer);exit;

	}
	
	public function get_order1(){
		
		$orderdate_Search = date('Ymd',time()-24*3600);
		$orderdate_Search = '20200724';
		$born_datebeginSearch = $orderdate_Search;
		$born_dateendSearch = $orderdate_Search;
		$sale = 'http://10.64.1.88/co/sale/';
		
		$num = '100';
		$page = 1;
		$gurl = $sale.'co_query_init.cmd?_pagesize='.$num.'&_currentpageindex='.$page.'&_gridinfoprimarykey=&flag=&reSearch=1';
	
		$postdata = array();
		$postdata['born_datebeginSearch'] = $born_datebeginSearch;			//查询日期
		$postdata['born_dateendSearch'] = $born_dateendSearch;			//查询日期
		
		$res = $this->data_page1($gurl,$postdata);
		
		$total_page_num = $this->yx_data->total_page_num($res);
		//a($total_page_num);exit;
		$total_page = $total_page_num['page'];
		//$total_page =  15;
		for($p = 1;$p<= $total_page;$p++){
			//获取订单跟进列表
			$num = '100';
			$page = $p;
			$gurl = $sale.'co_query_init.cmd?_pagesize='.$num.'&_currentpageindex='.$page.'&_gridinfoprimarykey=&flag=&reSearch=1';
		
			$postdata = array();
			$postdata['born_datebeginSearch'] = $born_datebeginSearch;			//查询日期
			$postdata['born_dateendSearch'] = $born_dateendSearch;				//查询日期
			//$postdata['cust_idSearch'] = '411302107078';						//许可证号

			//获取订单列表数据
			$res = $this->data_page1($gurl,$postdata);
			$list = $this->yx_data->order_list1($res);
			//a($list);exit;
			if(!$list){
				continue;
			}
			foreach($list as $v){
				//获取订单详情数据
				$url = $sale.'co.cmd?method=detail&primaryKey='.$v['number'];
				
				$res = $this->data_page($url);
				$detail = $this->yx_data->order_details($res);
				//a($detail);exit;
				$this->yx_data->insert_order($detail,$v,$p);
				
				$content = date('H:i:s').'     '.$detail['customer']['客户名称：'].'<br />';
				$this->yx_data->push_div_content('.scorpall',$content);
				
			}
			
			$this->yx_data->update_order_log($total_page_num,$p,$v['date']);
			
		}
		
	}
	
	//获取商户诚信等级		http://121.42.139.124/xcyc2/zdata_interface/data-getcustomerinfo
	public function getcustomerinfo(){
		$url = 'http://10.64.1.88/rs/greport/greport.cmd?method=getReportData';
		$post = array();
		$post['reportCode'] = '46e0f5b0-87e1-4ae5-961b-71395f4fe619';
		$post['searcherDescription'] = '';
		$post['COM_ID_NAME'] = '';
		$post['COM_ID'] = '';
		$post['COM_ID_BACKUPS'] = '';
		$post['SALE_CENTER_ORG_ID'] = '';
		$post['isSelectCustSearch'] = 'N';
		$post['hiddenInputForTitle'] = '报表查询';
		$post['custFilterSqlSearch'] = 'U0VMRUNUJTIwQ1VTVF9JRCUyMEZST00lMjBDT19DVVNUJTIwV0hFUkUlMjAxJTIwJTNEJTIwMSUyMCUyMEFORCUyMENPTV9JRCUyMElOJTIwKCcxMTQxMTMwMScpJTIw';
		
		$res = $this->data_page1($url,$post);
		
		if($res){
			$res = json_decode($res);
		}
		//a($res);exit;
		if($res->tableBodyData){
			$content = date('H:i:s').'---------更新商户诚信等级和常用信息---<br /><br />';
			$this->yx_data->push_div_content('.scorpall',$content);
			
			foreach($res->tableBodyData as $v){
				
				$this->yx_data->updatecustomerinfo($v);
				
				$content = date('H:i:s').'     '.$v[1]->value.'-----'.$v[3]->value.'------'.$v[4]->value.'------'.$v[7]->value.'<br />';
				$this->yx_data->push_div_content('.scorpall',$content);
			}
		}
		
	}
	
	/************************页面数据获取************************/
	//获取数据页面
	public function data_page($gurl){
		
		$res = $this->p->curl($gurl,'GET',null,false,1); 
		
		//如果需要点击继续模拟点击事件
		if(strpos($res,'Since your browser does not support JavaScript')){
			$this->judge_click($res);
			$res = $this->p->curl($gurl,'GET',null,false,1); 
		}

		return $res;
	}
	
	//获取数据页面
	public function data_page1($gurl,$postadta){
		
		$res = $this->p->curl($gurl,'POST',$postadta,false,1); 
		
		//如果需要点击继续模拟点击事件
		if(strpos($res,'Since your browser does not support JavaScript')){
			$this->judge_click($res);
			$res = $this->p->curl($gurl,'POST',$postadta,false,1); 
		}

		return $res;
	}
	
	//提交点击事件
	public function judge_click($form_psge){
		//第一次判断点击事件
		$form = $this->get_page_form_data($form_psge);
		
		$home_url = $this->ip.$form[0]['action'];
		$home_post = array();
		$home_post['RelayState'] = $form[0]['inputs'][0]['value'];
		$home_post['SAMLRequest'] = $form[0]['inputs'][1]['value'];
		
		$home_res = $this->p->curl($home_url,'POST',$home_post,false,1);
		
		//第二次判断点击事件
		$form = $this->get_page_form_data($home_res);
		
		$i_url = $this->ip.@$form[0]['action'];
		$i_post = array();
		$i_post['RelayState'] = @$form[0]['inputs'][0]['value'];
		$i_post['SAMLRequest'] = @$form[0]['inputs'][1]['value'];
		$i_post['SAMLResponse'] = @$form[0]['inputs'][2]['value'];
		
		$this->p->curl($i_url,'POST',$i_post,false,2);	//保存cookie
	}
	
	//获取表单数据
	function get_page_form_data($content){
		$arr_form = array();
		$form = $this->regular_form_tags($content);
		for($i = 0;$i < count($form[0]);$i ++){
			$arr_form[$i]['action'] = $this->regular_form_action($form[1][$i]);
			$arr_form[$i]['method'] = $this->regular_form_method($form[1][$i]);
			$input = $this->regular_input_tags($form[2][$i]);
			for($j = 0;$j < count($input[0]);$j ++)
			{
			$arr_form[$i]['inputs'][$j]['name'] = $this->regular_input_name($input[0][$j]);
			$arr_form[$i]['inputs'][$j]['type'] = $this->regular_input_type($input[0][$j]);
			$arr_form[$i]['inputs'][$j]['value'] = $this->regular_input_value($input[0][$j]);
			}
			$textarea = $this->regular_textarea_tags($form[2][$i]);
			for($k = 0;$k < count($textarea);$k ++)
			{
			$arr_form[$i]['textarea'][$k]['name'] = $this->regular_textarea_name($textarea[$k]);
			$arr_form[$i]['textarea'][$k]['value'] = $this->regular_textarea_value($textarea[$k]);
			}
			$select = $this->regular_select_tags($form[2][$i]);
			for($l = 0;$l < count($select[0]);$l ++)
			{
			$arr_form[$i]['select'][$l]['name'] = $this->regular_select_name($select[1][$l]);
			$option = $this->regular_option_tags($select[2][$l]);
			for($n = 0;$n < count($option[$l]);$n ++){
			$arr_form[$i]['select'][$l]['option'][$n] = $this->regular_option_value($option[$l][$n]);
			} 
			}
		}
		return $arr_form;
	}
	
	
	// 正则匹配 form 标签的 action 属性值
	function regular_form_action($string){
		$pattern = '/action[\s]*?=[\s]*?([\'\"])(.*?)\1/';
		if(preg_match($pattern,$string,$result)){
			return $result[2];
		}
		return null;
	}
	
	// 正则匹配 form 标签
	function regular_form_tags($string){ 
		$pattern = '/<form(.*?)>(.*?)<\/form>/si'; 
		preg_match_all($pattern,$string,$result);
		return $result;
	}
	
	// 正则匹配 form 标签的 method 属性值
	function regular_form_method($string)
	{
	$pattern = '/method[\s]*?=[\s]*?([\'\"])(.*?)\1/';
	if(preg_match($pattern,$string,$result))
	{
	return $result[2];
	}
	return null;
	}
	// 正则匹配 input 标签
	function regular_input_tags($string)
	{
	$pattern = '/<input.*?\/?>/si'; 
	if(preg_match_all($pattern,$string,$result))
	{
	return $result; 
	}
	return null;
	}
	// 正则匹配 input 标签的 name 属性值
	function regular_input_name($string)
	{
	$pattern = '/name[\s]*?=[\s]*?([\'\"])(.*?)\1/';
	if(preg_match($pattern,$string,$result))
	{
	return $result[2];
	}
	return null;

	}
	// 正则匹配 input 标签的 type 属性值
	function regular_input_type($string)
	{
	$pattern = '/type[\s]*?=[\s]*?([\'\"])(.*?)\1/';
	if(preg_match($pattern,$string,$result))
	{
	return $result[2];
	}
	return null;
	}
	// 正则匹配 input 标签的 value 属性值
	function regular_input_value($string)
	{
	$pattern = '/value[\s]*?=[\s]*?([\'\"])(.*?)\1/';
	if(preg_match($pattern,$string,$result))
	{
	return $result[2];
	}
	return null; 
	}
	// 正则匹配 textarea 标签
	function regular_textarea_tags($string)
	{
	$pattern = '/(<textarea.*?>.*?<\/textarea[\s]*?>)/si'; 
	if(preg_match_all($pattern,$string,$result))
	{
	return $result[1];
	}
	return null;
	}
	// 正则匹配 textarea 标签的 name 属性值
	function regular_textarea_name($string)
	{
	$pattern = '/name[\s]*?=[\s]*?([\'\"])(.*?)\1/si'; 
	if(preg_match($pattern,$string,$result))
	{
	return $result[2];
	}
	return null;
	}
	// 正则匹配 textarea 标签的 name 属性值
	function regular_textarea_value($string)
	{
	$pattern = '/<textarea.*?>(.*?)<\/textarea>/si'; 
	if(preg_match($pattern,$string,$result))
	{
	return $result[1];
	}
	return null;
	}
	// 正则匹配 select 标签
	function regular_select_tags($string)
	{
	$pattern = '/<select(.*?)>(.*?)<\/select[\s]*?>/si';
	preg_match_all($pattern,$string,$result);
	return $result;
	}
	// 正则匹配 select 标签的 option 子标签
	function regular_option_tags($string)
	{
	$pattern = '/<option(.*?)>.*?<\/option[\s]*?>/si';
	preg_match_all($pattern,$string,$result);
	return $result; 
	}
	// 正则匹配 select 标签的 name 属性值
	function regular_select_name($string)
	{
	$pattern = '/name[\s]*?=[\s]*?([\'\"])(.*?)\1/si';
	if(preg_match($pattern,$string,$result))
	{
	return $result[2];
	}
	return null;
	}
	// 正则匹配 select 的子标签 option 的 value 属性值
	function regular_option_value($string)
	{
	$pattern = '/value[\s]*?=[\s]*?([\'\"])(.*?)\1/si';
	if(preg_match($pattern,$string,$result))
	{
	return $result[2];
	}
	return null;
	}
	
	/******************登陆及获取cookie***************************/
	//登陆获取cookie
	public function index($j_username,$j_password){
		$this->login_page();
		$rdmCode = $this->rdmCode();
		$this->login($j_username,$j_password,$rdmCode);
	}
	
	//获取登录页面cookie
	public function login_page(){
		$url = $this->v6.'jsp/login/login.jsp';
		$res = $this->p->curl($url,'GET',null,false,2);
//		a($res);exit;
	}
	
	//获取随机串
	public function rdmCode(){
		$url = $this->v6.'rdmCode';
		$res = $this->p->curl($url,'GET',null,false,1);
		$res = substr($res,7,-2);
		
		return $res;
	}
	
	//登录
	public function login($j_username,$j_password,$rdmCode){
		$url = $this->v6.'j_bsp_security_check/';
		
		$user = array();
		
		$post_data = array();
		$post_data ['j_username'] = $j_username;
		$post_data ['j_password'] = $j_password;
		$post_data ['rdmCode'] = $rdmCode;

		$header = array();
		$header['Referer'] = $this->v6.'jsp/login/login.jsp';
		$header['Upgrade-Insecure-Requests'] = '1';
		$header['Accept'] = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
		$header['Accept-Encoding'] = 'gzip, deflate';
		$header['Accept-Language'] = 'zh-CN,zh;q=0.8';
		$header['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36';
		
		$res = $this->p->curl($url,'POST',$post_data,false,1);
		//a($post_data);exit;
	}
	
}
?>